

#include "MCU_include.h"

void System_Init()
{
	u8 temp;
	SYS_Clk(SYS_CLK_FH);	//system clock select FH=16MHz
	Clean_Ram();			//clear RAM bank
	SysWDT(WDT_1s);			//watch dog set 1S overflow
	GPIO_Init();			//GPIO initialization
	CnX_Select(0);			//TMCCKO from ovp0int
	PWM_Init();				//PWM function initialization
	OVP_Init();				//OVP initialization
	temp = OPA_Calibration();		//OPA calibration
	OPA_Init(EXTERN_AMP,DISABLE_RATIO_DOUBLE,R2_R1_Ratio30,temp);//20	
	IIC_Init();				//IIC initialization
	ADC_Init();				//ADC initialization
	CTM_Init();				//Timer initialization
	TB0_Init();				//timebase initialization
	Interrupt_Init();		//interrupt initialization	
	PHASE_Capture_Init2(0b00000000);//PHASE Capture and TMC initialization
//	HF_Init();				//PWM_HF mode initialization
}


void PWM_Init()
{
	PWM_Safe_protection_Init(MAX_FRE,MIN_FRE,DEC_PWM_Period);//Maximum frequency; Minimum frequency; Automatically adjust step size;
	Phase_Protection_Init(PHASE0,DELAY_2_PWMP,MIN_PHASE_WIDTH);//Phase source; Phase detection delay; Minimum phase width
	DTS0_1_Setting(DEAD0_1_TIME);		//Dead time of PWM0 and PWM1- unit ns
	DT_ON();							//Enable insertion of dead time
	PWM_MASK_Init(0,0);					///After PWM is automatically turned off, pins pwm0 and pwm1 output 0
	PWM_Freq_Init(1,d_PWM_F);			//The PWM clock source is Fpwm (32MHz), where 1 represents no frequency division; The initial frequency is d_PWM_f
	GCC_DELAY(1);
}


void OVP_Init()
{
	unsigned char Calibration_Value = 0;
	
	Calibration_Value = OVP_Calibration(OVP0,OVP0DA);
		_ovp0da=OVP0DA;			
		_ovp0c0=0b10100011;
		_ovp0c1=0b00000000;
		 _ovp0c1 |= Calibration_Value;
		_ovp0c2=0b00000001;
		_ovp0f=0;
		_nop();
	
	Calibration_Value = OVP_Calibration(OVP1,OVP1DA);
        _ovp1da = OVP1DA;
        _ovp1c0 = 0b10100001;
        _ovp1c1 = 0b00000000;
        _ovp1c1 |= Calibration_Value;
        _ovp1c2 = 0b00000001;			//OVP0 at OVP1 N   	     
        _ovp1f  = 0;

	Calibration_Value = OVP_Calibration(OVP2,OVP2DA);
		_ovp2da=OVP2DA;			
		_ovp2c0=0b10100011;			// 11 => 8*1/fSYS = 0.5uS
		_ovp2c1 |= Calibration_Value;
		_ovp2c2=0b00000000;
		_ovp2f=0;
	    _nop();

	Calibration_Value = OVP_Calibration(OVP4,OVP4DA);
		_ovp4da=OVP4DA;			
		_ovp4c0=0b10100011;			// 11 => 8*1/fSYS = 0.5uS
		_ovp4c1 |= Calibration_Value;
		_ovp4c2=0b00000000;
		_ovp4f=0;
	    _nop();
	    	
	Calibration_Value = OVP_Calibration(OVP6,OVP6DA);
		_ovp6da=OVP6DA;			
		_ovp6c0=0b10100101;
		_ovp6c1 |= Calibration_Value;
		_ovp6c2=0b00000001;
		_ovp6f=0;
	    _nop();
    
 	Calibration_Value = OVP_Calibration(OVP7,OVP7DA);   
		_ovp7da=OVP7DA;			
		_ovp7c0=0b11100110;
		_ovp7c1 |= Calibration_Value;
		_ovp7c2=0b00000001;
		_ovp7f=0;
	    _nop();

	Calibration_Value = OVP_Calibration(OVP8,OVP8DA);
		_ovp8da=OVP8DA;			
		_ovp8c0=0b10100111;
		_ovp8c1 |= Calibration_Value;
		_ovp8c2=0b00000000;
		_ovp8f=0;
	    _nop();
		_ovp8en=1;	
		GCC_DELAY(1);

}

/********* System clock selection*********/
void SYS_Clk(unsigned char SYS_Clock)
{
	_scc &= 0b00011111;
	_scc |= SYS_Clock;
}



////***********WDT Set******************//
void SysWDT(unsigned char WDT_Time)
{
	_wdtc = WDT_Time;
}

void CnX_Select(unsigned char  Cxs)
{
	_cxs = Cxs;
}


//*************clear RAM**************//
void Clean_Ram()
{
	_mp1h=0;
	_mp1l=0x80;
	for(_tblp=0;_tblp<128;_tblp++)	//bank0
	{
		_iar1=0;
		_mp1l++;	
	}
	_mp1h=1;
	_mp1l=0x80;
	for(_tblp=0;_tblp<128;_tblp++)	//bank1
	{
		_iar1=0;
		_mp1l++;	
	}	
	_mp1h=2;
	_mp1l=0x80;
	for(_tblp=0;_tblp<128;_tblp++)	//bank2
	{
		_iar1=0;
		_mp1l++;	
	}		
	_mp1h=3;
	_mp1l=0x80;
	for(_tblp=0;_tblp<128;_tblp++)	//bank3
	{
		_iar1=0;
		_mp1l++;	
	}		
	_mp1h=0;
}


/*********** GPIO initialization **********/
void GPIO_Init()
{

	_papu= 0xEF;	//PA0~PA3,PA5~PA7 pullhigh disable,PA4 pullhigh enable
	_pawu= 0x00;	//PA Wake up Disable,
	_pac = 0x30;	//PA0~PA3,PA6~PA7 set output mode,PA4~PA5 set input mode
	_pa  = 0xAF;	//
	
//// PA0 Set  OCDSDA //SCL
	_pas01 = 0;
	_pas00 = 0 ; 
	_pac0=0;


		
//// PA2 Set  OCDSCK    //SDA 
	_pas05 = 0;
	_pas04 = 0 ; 
	_pac2=0;
        
//PA1 Set   //OVP145P       
	_pas03 = 0;
	_pas02 = 1;           
            
//pa3 set      //OVP6P	 
	_pas07 = 0 ;
	_pas06 = 1 ;  
         
//pa4 set       //OVP7P& AN9
	_pas11 = 1 ;//11
	_pas10 = 1 ;
	
  
//PA5 Set      	//OVP8P & AN8
	_pas13 = 1;
	_pas12 = 1;   
        
//pa6 set pa6	IO
	_pas15=0;
	_pas14=0;
    _pac6 = 0;
                     
  
////PA7 Set IO output        
	_pas17 = 0;
	_pas16 = 1;  //1:TMCCKO for debug   
	_pac7=0;
	
	
//PBC =0x00,PB =0xFF,PA output High,
//PBPU =0x00,PA pull up Disable,
	_pbpu= 0b11000100;	//
	_pbc = 0b00101011;	//
	_pb  = 0b11101111;	//
	
       	
//PB7 Set SCK/SCL		
	_pbs17=1;
	_pbs16=0;            
        
//PB6 Set SDI/SDA
	_pbs15=0;
	_pbs14=1;  


//PB5 Set AN5
	_pbs13=1;		//10
	_pbs12=0;  
       
//PB4 Set PB4	IO
	_pbs11=0;
	_pbs10=0; 
	_pbc4=0;      
                
//PB3 Set AN3	
	_pbs07=0;
	_pbs06=1;  
	
//PB2 AS PHASE   output  
	_pbs05 = 0;		
	_pbs04 = 1;		//1:PHASE for debug   
	_pbc2=0;
	
////PB2 Set AN2      &AN2 
//	_pbs05 = 1;
//	_pbs04 = 0; 	 	
	
//PB1 Set OPARO	OPA output
	_pbs03=1;
	_pbs02=0;	
	
//PB0 Set OPINN1	
	_pbs01=0;
	_pbs00=1;	                                       


/************* PC port set ****************/
	_pcpu= 0b00001111;	//PC pull up enable,  
	_pcc = 0b00001100;	//PC0,1 =output, PC2,3=input
	_pc  = 0b00001110;	//PC0 set low,PC1,2,3 set high

//PC3 Set OVP026P1N      //OVP023P1N
	_pcs07 = 0 ;
	_pcs06 = 1 ;   
        
// PC2 Set 	buzzer		
	_pcs05 = 1 ;			//1  SDA/SDI/RX  for hardware UART RX	
	_pcs04 = 1 ;   	
//	_pcc2	= 0;
      
        
// PC1 Set PC1 for software UART TX	/ IO
#ifdef UART_ASCII
   // //PC1 Set IO
	_pcs03 = 0 ;
	_pcs02 = 0 ; 
	_pcc1=0;   
#else 	
// PC1 Set SDO/TX for hardware UART TX
	_pcs03 = 0 ;	//1
	_pcs02 = 0 ; 	//1
		_pcc1=0;   
#endif	
// PC0 Set PC0 as fan control
	_pcs01 = 0 ;
	_pcs00 = 0 ;
}


/******************* End of file ***************/